package classes{
	import classes.utils.Utility;
	import classes.utils.Validator;

	[Bindable]
	public class Account401K extends Account
	{
		public var maxContribution:Number = 16000;
		public var taxPercent:Number;
		public var maxMatch:Number;
		public var compMatch:Number;
		public var birthDay:Number;
		public var date:Date;
		public var sum:Number = 0;
		public var retirementAge:Boolean = false;
		public var receive:Number = 0;
		public var holder1:Number;
		public var holder2:Number;
		public var birthMonth:Number;
		public var birthYear:Number;
		public function Account401K()
		{
			accountType = "401k";
			
		}
		
		public function calcAnnualMax():Number{
			date = new Date();
			//generalAlert(date.fullYear.toString());
			var difference:Number;
			if(date.fullYear > 2008){
				difference = date.fullYear - 2008;
				maxContribution = 16000 + (500*difference);
			}
			//generalAlert(maxContribution.toString());
			return maxContribution;
		}
		//public function setUserContribution():Number{
		//		userContribution = 
		//}
		//public function setCompanyContribution():Number{
		//}
		public function addStartTransaction(newTransaction:Transaction):void{
			if(!Validator.validateTransaction(newTransaction)){
				//TODO: display a message?
			}
			//generalAlert(newTransaction.amount.toString());
			if((newTransaction.amount + balance) > maxContribution){
					generalAlert("New Transaction Amount will cause balance to exceed Maximum Annual 401k contribution.");
			}
			else{
			if(newTransaction.isScheduled)
				this.scheduledTransactions.addItem(newTransaction);
			else
				this.transactions.addItem(newTransaction);
			
			if(TreasuryModel.getInstance().addTransaction != null)
				TreasuryModel.getInstance().addTransaction.refresh();
				
			TreasuryModel.getInstance().recalculateUserData();
			}
				return;
		}
		
		override public function addTransaction(newTransaction:Transaction):void{
			if(newTransaction.isDeposit == false){
				if(retirementAge == false && (newTransaction.amount*-1) <= balance ){
					holder1 = newTransaction.amount*(taxPercent/100);
					holder2 = newTransaction.amount*0.10;
					receive = (newTransaction.amount - (holder1 + holder2))*(-1);
					generalAlert("If the current day is not yet " + this.birthMonth+ "/" + this.birthDay + "/" + (60+this.birthYear) +" then you are not yet 60 and you will receive a %10 penalty in addition to a deduction of %" + taxPercent + " for taxes.  After these deductions you will receive $" + receive + "\n\nIf you are over 60, then you will receive $" + ((newTransaction.amount - holder1)*-1) + " after taxes.");
					addHelper(newTransaction);
				}
				else if((newTransaction.amount*-1) > balance){
					generalAlert("You are trying to take out more than you have in this account!");
				}				
				else{
					addHelper(newTransaction);
					//generalAlert("Day: " + this.birthDay + " Month: " + this.birthMonth + " Year: " + this.birthYear);
				}
			}
			else{
				addHelper(newTransaction);
				//generalAlert("Day: " + this.birthDay + " Month: " + this.birthMonth + " Year: " + this.birthYear);
			}
			return;
		}
		
		public function addHelper(newTransaction:Transaction):void{
			if(!Validator.validateTransaction(newTransaction)){
				//TODO: display a message?
				return;
			}
			if(newTransaction.amount >= 0){
				userContribution = newTransaction.amount;
				compContribution = (compMatch/100)*userContribution;
				newTransaction.amount += compContribution;
				sum += compContribution;
				if(sum > maxMatch){
					if((sum - compContribution) < maxMatch){
						compContribution = compContribution - (sum - maxMatch); 
						generalAlert("Maximum Company Match has been exceeded.");
						sum = maxMatch;
					}
					else{
						generalAlert("Maximum Company Match has been exceeded.");
						compContribution = 0;
						newTransaction.amount = userContribution;
					}
				}
				generalAlert("User Contribution: $" + userContribution + "\nCompany Contribution: $" + compContribution);
			}
		
				//generalAlert(newTransaction.amount.toString());
				if((newTransaction.amount + balance) > maxContribution){
						generalAlert("New Transaction Amount will cause balance to exceed Maximum Annual 401k contribution.");
				}
				else{
				if(newTransaction.isScheduled)
					this.scheduledTransactions.addItem(newTransaction);
				else
					this.transactions.addItem(newTransaction);
			
				if(TreasuryModel.getInstance().addTransaction != null)
					TreasuryModel.getInstance().addTransaction.refresh();
				
				TreasuryModel.getInstance().recalculateUserData();
				}
			
			
				return;
		}
		
		public function calcRetirementAge():void{
			
		}
		
		private function generalAlert(words2:String):void{
						Utility.getInstance().showAlert(words2, "Attention");		
					
			}
		override public function calculateBalance():Number{
			this.balance = 0;
			for(var i:int = 0; i < this.transactions.length; i++){
				this.balance += this.transactions.getItemAt(i).amount;
			}
			return this.balance;
		}
		
	}
	
}